package com.thread;

import java.util.LinkedList;

public class Storage implements AbstractStorage{
    private final int MAX_SIZE = 100;
    private LinkedList list = new LinkedList();

    @Override
    public void consume(int num) {
        synchronized (list){
            while (list.size()<num){
                System.out.println(Thread.currentThread().getId()+"consume 【要消费的产品数量】:" + num + "\t【库存量】:"+ list.size() + "\t暂时不能执行消费任务!");
                try {
                    list.wait(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            for(int i=0;i<num;i++){
                list.remove();
            }
            System.out.println(Thread.currentThread().getId()+"【已经消费产品数】:" + num + "\t【现仓储量为】:" + list.size());

            list.notifyAll();
        }
    }

    @Override
    public void product(int num) {
        synchronized (list){
            while(list.size()+num > MAX_SIZE){
                System.out.println(Thread.currentThread().getId()+"【要生产的产品数量】:" + num + "\t【库存量】:" + list.size() + "\t暂时不能执行生成任务!");
                try {
                    list.wait(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            for(int i=0;i<num;i++){
                list.add(new Object());
            }

            System.out.println(Thread.currentThread().getId()+"【已经生产产品数】:" + num + "\t【现仓储量为】:" + list.size());
            list.notifyAll();
        }
    }
}
